#include "selectzoneui.h"
#include "dcuinformation.h"
#include <QDebug>
#include <QTimer>

SelectZoneUI::SelectZoneUI(QWidget *parent) : QWidget(parent)
{
    setWindowFlags(Qt::FramelessWindowHint);
    setAutoFillBackground(true);
    background = palette();
    background.setBrush(QPalette::Window, QBrush(QPixmap(":/images/defaultUI.png")));
    setPalette(background);

    initTopView();
    initSelectGroupBtn();
    initSelectGroupLabel();
    initSelectGroupTrialBtn();
    initNumBtn();
}

void SelectZoneUI::initTopView()
{
    QLabel *label = new QLabel(this);
    label->setText(tr("站名："));
    label->setFont(QFont(ZITI, 18, QFont::Normal));
    label->setGeometry(QRect(160, 10, 60, 35));
    label->setStyleSheet("color:black;");

    QLabel *stationText = new QLabel(this);
    stationText->setFont(QFont(ZITI, 15, QFont::Normal));
    stationText->setGeometry(QRect(220, 12, 180, 35));
    stationText->setStyleSheet("color:black;background:white");

    QPushButton *lineBtn = new QPushButton(this);
    lineBtn->setText("线路广播");
    lineBtn->setGeometry(QRect(470, 12, 150, 50));
    lineBtn->setFont(QFont(ZITI, 18, QFont::Normal));
    lineBtn->hide();

    QPushButton *backBtn = new QPushButton(this);
    backBtn->setText("返回");
    backBtn->setGeometry(QRect(620, 370, 150, 50));
    backBtn->setFont(QFont(ZITI, 18, QFont::Normal));
    connect(backBtn, SIGNAL(clicked()), this, SLOT(backBtnDone()));

    QPushButton *btn = new QPushButton(this);
    btn->setCheckable(true);
    btn->setFont(QFont(ZITI, 18, QFont::Normal));
    btn->setGeometry(QRect(10, 40, 50, 50));
    btn->setStyleSheet(
        "QPushButton{border-image:url(:/images/noCheck.png);color:rgb(19,60,76);text-align:center}"
        "QPushButton:checked{border-image:url(:/images/isCheck.png);}");

    QLabel *selectAll = new QLabel(this);
    selectAll->setText(tr("全选"));
    selectAll->setFont(QFont(ZITI, 18, QFont::Normal));
    selectAll->setGeometry(QRect(70, 47, 60, 35));
    selectAll->setStyleSheet("color:black;");

    QPushButton *upBtn = new QPushButton(this);
    upBtn->setText("上一页");
    upBtn->setGeometry(QRect(130, 49, 100, 50));
    upBtn->setFont(QFont(ZITI, 18, QFont::Normal));
    connect(upBtn, &QPushButton::clicked, this, &SelectZoneUI::upPageDone);

    pageLabel = new QLabel(this);
    pageLabel->setText(tr("1/1"));
    pageLabel->setFont(QFont(ZITI, 18, QFont::Normal));
    pageLabel->setGeometry(QRect(230, 54, 70, 50));
    pageLabel->setAlignment(Qt::AlignHCenter);
    pageLabel->setStyleSheet("color:white;");

    QPushButton *downBtn = new QPushButton(this);
    downBtn->setText("下一页");
    downBtn->setGeometry(QRect(300, 49, 100, 50));
    downBtn->setFont(QFont(ZITI, 18, QFont::Normal));
    connect(downBtn, &QPushButton::clicked, this, &SelectZoneUI::downPageDone);
}

void SelectZoneUI::initSelectGroupBtn()
{
    int total = 6;
    for (int var = 0; var < total; ++var)
    {
        QPushButton *btn = new QPushButton(this);
        btn->setCheckable(true);
        btn->setFont(QFont(ZITI, 18, QFont::Normal));
        btn->setGeometry(QRect(10, 90 + (50 + 5) * var, 50, 50));
        btn->setStyleSheet(
            "QPushButton{border-image:url(:/images/noCheck.png);color:rgb(19,60,76);text-align:center}"
            "QPushButton:checked{border-image:url(:/images/isCheck.png);}");
        connect(btn, SIGNAL(clicked()), this, SLOT(currentSelectBtn()));
        selectBtnGroup.append(btn);
    }
}

void SelectZoneUI::initSelectGroupLabel()
{
    int total = 6;
    for (int var = 0; var < total; ++var)
    {
        QLabel *label = new QLabel(this);
        label->setText(tr("0：离线"));
        label->setAlignment(Qt::AlignCenter);
        label->setFont(QFont(ZITI, 15, QFont::Normal));
        label->setGeometry(QRect(70, 100 + (50 + 5) * var, 330, 35));
        label->setStyleSheet("color:black;background:white");
        selectLabelGroup.append(label);
    }
}

void SelectZoneUI::initSelectGroupTrialBtn()
{
    int total = 6;

    for (int var = 0; var < total; ++var)
    {
        QPushButton *btn = new QPushButton(this);
        btn->setCheckable(true);
        btn->setFont(QFont(ZITI, 18, QFont::Normal));
        btn->setGeometry(QRect(410, 95 + (50 + 5) * var, 50, 50));
        btn->setStyleSheet(
            "QPushButton{border-image:url(:/images/noTrial.png);color:rgb(19,60,76);text-align:center}"
            "QPushButton:checked{border-image:url(:/images/isTrial.png);}");
        connect(btn, SIGNAL(clicked()), this, SLOT(trialGroupBtn()));
        trialBtnGroup.append(btn);
    }
}

void SelectZoneUI::initNumBtn()
{
    int total = 11;

    QStringList textList;
    textList << "9"
             << "8"
             << "7"
             << "4"
             << "5"
             << "6"
             << "1"
             << "2"
             << "3"
             << "0"
             << "清除";
    QLabel *label = new QLabel(this);
    label->setText(tr("试听"));
    label->setFont(QFont(ZITI, 20, QFont::Normal));
    label->setGeometry(QRect(470, 100, 60, 35));
    label->setStyleSheet("color:black");

    inputNum = new QLabel(this);
    inputNum->setFont(QFont(ZITI, 15, QFont::Normal));
    inputNum->setGeometry(QRect(530, 100, 180, 35));
    inputNum->setStyleSheet("color:black;background:white");

    selfMonitorBtn = new QPushButton(this);
    selfMonitorBtn->setCheckable(true);
    selfMonitorBtn->setFont(QFont(ZITI, 18, QFont::Normal));
    selfMonitorBtn->setGeometry(QRect(715, 98, 50, 50));
    selfMonitorBtn->setStyleSheet(
        "QPushButton{border-image:url(:/images/noTrial.png);color:rgb(19,60,76);text-align:center}"
        "QPushButton:checked{border-image:url(:/images/isTrial.png);}");
    connect(selfMonitorBtn, SIGNAL(clicked()), this, SLOT(selfCurrentSelectBtn()));

    for (int var = 0; var < total; ++var)
    {
        QPushButton *btn = new QPushButton(this);
        btn->setCheckable(true);
        btn->setText(textList.at(var));
        btn->setFont(QFont(ZITI, 18, QFont::Normal));
        btn->setGeometry(QRect(470 + (0 + 100) * (var % 3), 160 + (0 + 50) * (var / 3), 100, 50));
        if (btn->text() == "清除") btn->resize(200, 50);
        btn->setStyleSheet(
            "QPushButton{border-image:url(:/images/ButtonPulled.png);color:rgb(19,60,76);text-align:center}"
            "QPushButton:pressed{border-image:url(:/images/ButtonPushed.png);}");

        connect(btn, SIGNAL(clicked()), this, SLOT(numBtnDone()));
    }

    QPushButton *btn = new QPushButton(this);
    btn->setCheckable(true);
    btn->setText("语音播放");
    btn->setFont(QFont(ZITI, 18, QFont::Normal));
    btn->setGeometry(QRect(470, 12, 150, 50));

    btn->setStyleSheet(
        "QPushButton{border-image:url(:/images/ButtonPulled.png);color:rgb(19,60,76);text-align:center}"
        "QPushButton:pressed{border-image:url(:/images/ButtonPushed.png);}");
    connect(btn, &QPushButton::clicked, this, [=]() { emit playCmd(m_selectIdList, "play", inputNum->text()); });

    btn = new QPushButton(this);
    btn->setCheckable(true);
    btn->setText("语音停止");
    btn->setFont(QFont(ZITI, 18, QFont::Normal));
    btn->setGeometry(QRect(620, 12, 150, 50));

    btn->setStyleSheet(
        "QPushButton{border-image:url(:/images/ButtonPulled.png);color:rgb(19,60,76);text-align:center}"
        "QPushButton:pressed{border-image:url(:/images/ButtonPushed.png);}");
    connect(btn, &QPushButton::clicked, this, [=]() { emit playCmd(m_selectIdList, "stop", inputNum->text()); });
}

void SelectZoneUI::currentSelectBtn()
{
    QObject *    obj   = QObject::sender();
    QPushButton *btn   = qobject_cast<QPushButton *>(obj);
    int          index = (selectBtnGroup.indexOf(btn) + 1) + m_page * 6;
    if (btn->isChecked())
    {
        if (0 < index && index <= m_speakerIdList.size())
        {
            m_selectIdList.append(m_speakerIdList.at(index - 1));
        }

        qDebug() << "选中组" << selectBtnGroup.indexOf(btn) << index;
    }
    else
    {
        if (0 < index && index <= m_speakerIdList.size())
        {
            m_selectIdList.removeAll(m_speakerIdList.at(index - 1));
        }

        qDebug() << "取消组" << selectBtnGroup.indexOf(btn) << index;
    }
}

void SelectZoneUI::trialGroupBtn()
{
    QObject *    obj = QObject::sender();
    QPushButton *btn = qobject_cast<QPushButton *>(obj);

    foreach (QPushButton *var, trialBtnGroup)
    {
        if (var->isChecked())
        {
            if (btn != var)
            {
                var->setChecked(false);
            }
            else
            {
                var->setChecked(true);
            }
        }
    }

    if (btn->isChecked())
    {
        m_tryListenGroupId = (trialBtnGroup.indexOf(btn) + 1) + m_page * 6;
        resetSelfMonitorBtn();
        emit monitorOpenCmd(QString::number(m_tryListenGroupId), "play");

        qDebug() << "试听选中" << trialBtnGroup.indexOf(btn) << m_tryListenGroupId;
    }
    else
    {
        emit monitorOpenCmd(QString::number(m_tryListenGroupId), "stop");
        qDebug() << "试听取消" << trialBtnGroup.indexOf(btn);
    }
}

void SelectZoneUI::numBtnDone()
{
    QObject *    obj = QObject::sender();
    QPushButton *btn = qobject_cast<QPushButton *>(obj);
    if (btn->text() != "清除")
    {
        inputNum->setText(inputNum->text() + btn->text());
    }
    else
    {
        inputNum->setText("");
    }
}

void SelectZoneUI::backBtnDone()
{
    emit pageInfo("返回");
}

void SelectZoneUI::upPageDone()
{
    if (m_page > 0)
    {
        m_page--;
        emit zonePageInfo(m_page);
    }
}

void SelectZoneUI::downPageDone()
{
    if (m_page < m_maxPage - 1)
    {
        m_page++;
        emit zonePageInfo(m_page);
    }
}

void SelectZoneUI::selfCurrentSelectBtn()
{
    QObject *    obj = QObject::sender();
    QPushButton *btn = qobject_cast<QPushButton *>(obj);

    if (btn->isChecked())
    {
        resetSpeakerBtn();
        emit monitorOpenCmd("1", "selfPlay");
    }
    else
    {
        emit monitorOpenCmd("1", "selfStop");
    }
}

void SelectZoneUI::resetSpeakerBtn()
{
    foreach (QPushButton *btn, trialBtnGroup)  //复位扬声器监听按钮
    {
        if (btn->isChecked())
        {
            btn->setChecked(false);
            emit monitorOpenCmd(QString::number(m_tryListenGroupId), "stop");
        }
    }
}

void SelectZoneUI::resetSelfMonitorBtn()
{
    if (selfMonitorBtn->isChecked())
    {
        selfMonitorBtn->setChecked(false);
        emit monitorOpenCmd("1", "selfStop");
    }
}

void SelectZoneUI::setSelectGroupLabel(QStringList speakerList, QMap<QString, QString> map, int page)
{
    const int row = 6;

    setMaxPage(speakerList);

    pageLabel->setText(QString("%1/%2").arg(m_page + 1).arg(m_maxPage));

    for (int i = 0; i < row; ++i)
    {
        int index = i + page * row;
        if (index < speakerList.size())
        {
            selectLabelGroup.at(i)->setText(QString("%1：在线").arg(map.value(speakerList.at(index))));
        }
        else
        {
            selectLabelGroup.at(i)->setText(QString("0： 离线"));
        }
    }
}

void SelectZoneUI::setMaxPage(QStringList speakerList)
{
    if (speakerList.isEmpty()) return;

    m_speakerIdList = speakerList;

    if (speakerList.size() % 6 == 0)
    {
        m_maxPage = speakerList.size() / 6;
    }
    else
    {
        m_maxPage = speakerList.size() / 6 + 1;
    }
}

QString SelectZoneUI::getFileId()
{
    return inputNum->text();
}
